package org.demo.service.impl;


import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisViewUtil {

	public static void main(String[] args) {

		JedisPool jedisPool = new JedisPool("119.23.13.226", 6388);

		Jedis jedis = jedisPool.getResource();
		// 密码认证
		jedis.auth("123456");//关闭密码后，这句不能加，会报错ERR Client sent AUTH, but no password is set  
		jedis.set("name", "阿萨德");
		String string = jedis.get("name");
		System.out.println(string);
		System.out.println(jedis.get("name"));
		jedisPool.close();

//		long start = System.currentTimeMillis();
//
//		// 连接redis服务器，localhost:6379
//		Jedis redis = new Jedis("119.23.13.226", 6388);
//		redis.auth("123456");
//		// 获取所有key
//		Set<byte[]> keySet = redis.keys("*".getBytes());
//		byte[][] keys = keySet.toArray(new byte[keySet.size()][]);
//		// 获取所有value
//		byte[][] values = redis.mget(keys).toArray(new byte[keySet.size()][]);
//
//		// 打印key-value对
//		for (int i = 0; i < keySet.size(); ++i) {
//			System.out.println(byte2hex(keys[i]) + " --- " + byte2hex(values[i]));
//		}
//		long end = System.currentTimeMillis();
//		// 计算耗时
//		System.out.println("Query " + values.length + " pairs takes " + (end - start) + " millis");
//		redis.close();
	}

	private static String byte2hex(byte[] buffer) {
		String h = "0x";
		if (buffer != null && buffer.length > 0) {
			for (byte aBuffer : buffer) {
				String temp = Integer.toHexString(aBuffer & 0xFF);
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				h = h + " " + temp;
			}
		}
		return h;

	}
}
